<?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/**
 * Hockey extension for Joomleague
 * @version $Id: playersranking.php 111 2008-04-09 18:11:29Z julien.vonthron $
 * @package JlgHockeyExtension
 * @copyright Copyright (C) 2007 Julien Vonthron. All rights reserved.
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
 * JlgHockeyExtension is free software and parts of it may contain or be derived from the
 * GNU General Public License or other free or open source software licenses.
 * 
 * contact: julien.vonthron[at]gmail.com 
 *   
 * Joomleague is a League Management and Prediction Game for Joomla!
 * Homepage: http://www.joomleague.de 
 *  
 */
?>

<?php

  function getLast3RoundNames( &$joomleague, $last_matchcode = null )
  {
    if ( !$last_matchcode ) {
    	$last_matchcode = $joomleague->current_round;
    }
    
  	$query = sprintf(
      " SELECT distinct matchcode, id, name, round_date_first, round_date_last 
        FROM #__joomleague_rounds 
        WHERE project_id = %s ORDER BY matchcode ASC ", 
      $joomleague->id );
    
    $joomleague->_db->setQuery($query);
    $result = $joomleague->_db->loadObjectList();
    
    // now, we only want to keep the $last_matchcode, and the 2 just before
    $rounds = array();
    foreach ( $result AS $r )
    {
    	if ( $r->matchcode != $last_matchcode ) {
    		$rounds[] = $r;
    	}
    	else 
    	{
    		$rounds[] = $r;
    		$rounds = array_slice( $rounds, -3 );
    		break;
    	}
    }
    return $rounds;
  }

  function showLast3() 
  {
    global $jl_func, $database, $joomleague, $overall_config;
    
    $team_plan = intval( mosGetParam( $_REQUEST, 'tid', 0 ) );
    $divisionid = intval( mosGetParam( $_REQUEST, 'division', 0 ) );
    
    $showPlan_config=$joomleague->getTemplateConfig("show_plan.tpl");
    
    if ($divisionid > 0) {
      $division = new LeagueDivision($database);
      $division->load($divisionid);
    }
    
    //new for plan order
    $ordering=($showPlan_config['plan_order'])?$showPlan_config['plan_order']:"DESC";
    $rounds = getLast3RoundNames( $joomleague );
    
    $func=$jl_func;
    $favteams = explode(",", $joomleague->fav_team);
    
    //get project teams info
    $teams = $joomleague->getTeamsIndexedById();
 
    $htmlHelper = new HTML_joomleague();
    
    require(getTemplateFile(JL_TEMPLATE_PATH, 'show_last3.tpl.php'));
  }

?>
